I can't seem to get my POST api to work when it was working normally like 1 week ago, I think I forgot how to put the date but here is the error:
ValidationError: validTill: Cast to Number failed for value \"NaN\" at path \"validTill\"
Here is my json I am trying to send: {"plrID": "1", "key": "abc", "validTill": "1633872004841", "reason": "test"} is there something I am doing wrong here? I have tried doing it with a date but it didn't work. Here is the code for it: ```const mongoose = require('mongoose')
module.exports = async function (req, res) {
const receivedData = {...req.body}
try {
require('../models/Ban')
const BanModel = mongoose.model('Ban')
const pre = await BanModel.findOne({ plrID: receivedData.plrID})
if (pre) {
var someDate = new Date();
var numberOfDaysToAdd = receivedData.validDays;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
await BanModel.findByIdAndUpdate(pre._id, {$set: { validTill: someDate.getTime(), Valid: true }})
} else {
var someDate = new Date();
var numberOfDaysToAdd = receivedData.validDays;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
const newBanInstance = new BanModel({
plrID: receivedData.plrID,
key: receivedData.key,
validTill: someDate.getTime(),
reason: receivedData.reason,
gameID: 1,
Cancelled: false,
Valid: true
})
const newSub = await newBanInstance.save()
require('../models/BanCreator')
const creatorModel = mongoose.model('BanCreator')
await creatorModel.findOneAndUpdate({ gameID: 1 }, { $push: { BanIds: newSub._id } })
}
var nowDate = new Date();
var addDays = receivedData.validDays;
nowDate.setDate(nowDate.getDate() + addDays);
res.json({
plrID: receivedData.plrID,
validTill: nowDate.toDateString(),
reason: receivedData.reason,
valid: true,
failResponse: "Faced No Errors"
})
} catch (e) {
res.json({
plrID: receivedData.plrID,
validTill: null,
reason: receivedData.reason,
valid: false,
failResponse: `oopsies an error happened: ${e}`
})
}
}``` Thanks! Ben
I did the incorrect params, I ended up fixing it myself.